package com.cloud.thelen.wallpaper.AllAdapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.collection.SparseArrayCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

public class BaseRecyclerHolder extends RecyclerView.ViewHolder {
    private SparseArrayCompat<View> mViews;

    public BaseRecyclerHolder(@NonNull View itemView) {
        super(itemView);
        mViews = new SparseArrayCompat<>();
    }

    public <V extends View> V getView(@IdRes int res) {
        View v = mViews.get(res);
        if (v == null) {
            v = itemView.findViewById(res);
            mViews.put(res, v);
        }
        return (V) v;
    }

    public TextView getText(@IdRes int tvRes) {
        TextView textView = getView(tvRes);
        return textView;
    }

    public BaseRecyclerHolder setText(@IdRes int tvRes, String text) {
        TextView textView = getView(tvRes);
        textView.setText(text);
        return this;
    }

    public BaseRecyclerHolder setImage(@IdRes int imgRes, Context context, String url) {
        ImageView img = getView(imgRes);
        Glide.with(context).load(url).into(img);
        return this;
    }

    public BaseRecyclerHolder setImage(@IdRes int imgRes, int img) {
        ImageView iv = getView(imgRes);
        iv.setImageResource(img);
        return this;
    }

    public BaseRecyclerHolder setVisibility(@IdRes int res, boolean flag) {
        View iv = getView(res);
        iv.setVisibility(flag ? View.VISIBLE : View.GONE);
        return this;
    }

    public BaseRecyclerHolder setBackground(@IdRes int res, int id) {
        View iv = getView(res);
        iv.setBackgroundResource(id);
        return this;
    }

    public BaseRecyclerHolder setBackgroundColor(@IdRes int res, @ColorInt int color) {
        CardView cv = getView(res);
        cv.setCardBackgroundColor(color);
        return this;
    }

    public BaseRecyclerHolder setOnClickListener(@IdRes int id, View.OnClickListener listener) {
        View view = getView(id);
        view.setOnClickListener(listener);
        return this;
    }


}
